package com.qst.rongxiaoserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qst.rongxiaoserver.mapper.OrderMapper;
import com.qst.rongxiaoserver.model.OrderModel;
import com.qst.rongxiaoserver.model.ProductModel;
import com.qst.rongxiaoserver.service.IOrderService;
import com.qst.rongxiaoserver.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderModel> implements IOrderService {

    @Autowired
    private IProductService productService;

    @Override
    public boolean createOrder(OrderModel order) {
        // 生成订单号
        String oid =  new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        order.setOrderId("PT"+oid);    // 16位订单号

        // 如何获取商品的价格
        Integer pid = order.getProductId();

        ProductModel productDetail = productService.getById(pid);  // 根据商品id获取商品详情

        String priceStr = productDetail.getPrice();
        double price1 = Double.parseDouble(priceStr);

        order.setTotalPrice(price1 * order.getQuantity());

        return this.save(order);

    }

    // 支付订单
    @Override
    public boolean payOrder(String orderId) {
        // 1. 校验订单是否存在
        OrderModel order = this.getById(orderId);
        order.setPaymentStatus(5);
        return this.save(order);
    }
}
